package org.junv;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HandleLogin")
public class HandleLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");

		String loginName;
		String loginPass;
		loginName = request.getParameter("login_username").toString();
		loginPass = request.getParameter("login_password").toString();
		PrintWriter out = response.getWriter();
		// 对登录进行验证
		ResultSet myRs = null;

		// 查询数据库。。。。
		String sql = "use sp_eq_management SELECT * FROM userInfo where username='"
				+ loginName + "'";

		DBBean myBean = new DBBean();
		myBean.init();
		myRs = myBean.executeQuery(sql);

		try {
			if (myRs.next()) {
				System.out.println("有记录");
				System.out.println(myRs.getString("password"));
			} else {
				System.out.println("没有记录");
				out.println("登录失败,请输入正确的用户名和密码！");
				myBean.colse();
			}

		} catch (SQLException e) {

			System.out.println("返回出错。。。。");
			e.printStackTrace();
		}

		try {
			if (myRs.getString("password").equals(loginPass)) {
				System.out.println("登录成功");
				response.sendRedirect("back_home.html");
			} else {
				out.println("登录失败,请输入正确的用户名和密码！");
			}
		} catch (SQLException e) {
			System.out.println("呀失败了。。。。。");
			e.printStackTrace();
			myBean.colse();
		}
		myBean.colse();
		out.flush();
		out.close();
	}

}
